/**
 * 
 */
package info.niwota.profile;

import info.niwota.commons.web.JsonObject;
import info.niwota.profile.util.DeviceHelper;

import org.json.JSONException;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;

/**
 * @author qiangli
 * 
 */
public final class Preferences {
	public static final String PROFILE_ID = "p_id";
	//
	public static final String PROFILE_NAME = "p_name";
	public static final String PROFILE_NOTE = "p_note";
	public static final String PROFILE_PHOTO = "p_photo";
	public static final String PROFILE_LOCATION = "p_loc";
	public static final String PROFILE_TIME = "p_time";

	public static final String PROFILE_GENDER = "p_gender";
	public static final String PROFILE_DOB = "p_dob";
	public static final String PROFILE_WELCOME = "p_welcome";
	public static final String PROFILE_CALL = "p_call";
	public static final String PROFILE_TEXT = "p_text";
	public static final String PROFILE_EMAIL = "p_email";
	public static final String PROFILE_CHAT = "p_chat";

	public static final String PROFILE_WEBSITE = "p_website";
	public static final String PROFILE_ADDRESS = "p_address";

	public static final String PROFILE_MODIFIED = "p_modified";

	public static final String PROFILE_PHOTO_MODIFIED = "p_photo_modified";

	public static final String PROFILE_MOBILE = "p_mobile";
	public static final String PROFILE_GMAIL = "p_gmail";
	public static final String PROFILE_WIFI = "p_wifi";
	public static final String PROFILE_BLUETOOTH = "p_bt";

	public static final String PROFILE_SIGNATURE = "p_signature";

	//
	private static final String NEVER_MODIFIED = "0";

	private static final String PREFS_FAVORITE_PEERS = "favorite_peers";

	private static final String HTTP_PRIVATE_KEY = "http.private.key";

	public static final String MY_FRIENDS = "my.friends";

	public static final String MY_FRIENDS_DIRTY = "my.friends.dirty";

	// public static final String MY_STATUS = "my.status";
	// public static final String MY_PRESENCE = "my.presence";

	private Preferences() {
	}

	public static String getHttpPrivateKey(Context ctx) {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		return sp.getString(HTTP_PRIVATE_KEY, null);
	}

	public static void putHttpPrivateKey(Context ctx, String key) {
		final SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		Editor ed = pref.edit();
		ed.putString(HTTP_PRIVATE_KEY, key);
		ed.commit();
	}

	public static String getProfilePhoto(Context ctx) {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		return sp.getString(PROFILE_PHOTO, null);
	}

	public static void setProfilePhoto(Context ctx, String path) {
		final SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		Editor ed = pref.edit();
		ed.putString(PROFILE_PHOTO, path);

		long now = System.currentTimeMillis();
		ed.putString(PROFILE_MODIFIED, now + "");
		ed.putString(PROFILE_PHOTO_MODIFIED, now + "");
		ed.commit();
	}

	public static JsonObject getProfileJSONObject(Context ctx, boolean location)
			throws JSONException {
		JsonObject jo = new JsonObject();
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);

		jo.put(Preferences.PROFILE_ID,
				p.getString(Preferences.PROFILE_ID, null));

		jo.put(Preferences.PROFILE_NAME,
				p.getString(Preferences.PROFILE_NAME, ""));
		String loc = null;
		if (location && p.getBoolean(Preferences.PROFILE_LOCATION, false)) {
			loc = Locations.getLocation(ctx);
		}
		jo.put(Preferences.PROFILE_LOCATION, nvl(loc));
		String time = null;
		if (p.getBoolean(Preferences.PROFILE_TIME, false)) {
			time = Dates.getDateTime(ctx);
		}
		jo.put(Preferences.PROFILE_TIME, nvl(time));
		//
		jo.put(Preferences.PROFILE_GENDER,
				p.getString(Preferences.PROFILE_GENDER, "0"));
		jo.put(Preferences.PROFILE_DOB,
				p.getString(Preferences.PROFILE_DOB, ""));
		jo.put(Preferences.PROFILE_WELCOME,
				p.getString(Preferences.PROFILE_WELCOME, ""));
		//
		if (p.getBoolean(Preferences.PROFILE_MOBILE, false)) {
			jo.put(Preferences.PROFILE_MOBILE, DeviceHelper.getPhone(ctx));
		}
		if (p.getBoolean(Preferences.PROFILE_GMAIL, false)) {
			jo.put(Preferences.PROFILE_GMAIL, DeviceHelper.getGmail(ctx));
		}
		if (p.getBoolean(Preferences.PROFILE_WIFI, false)) {
			jo.put(Preferences.PROFILE_WIFI, DeviceHelper.getWifiMacAddress(ctx));
		}
		//
		//RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
		//if (p.getBoolean(Preferences.PROFILE_BLUETOOTH, false)) {
		//	jo.put(Preferences.PROFILE_BLUETOOTH, DeviceHelper.getBluetoothAddress());
		//}	
		//
		jo.put(Preferences.PROFILE_CALL,
				p.getString(Preferences.PROFILE_CALL, ""));
		jo.put(Preferences.PROFILE_TEXT,
				p.getString(Preferences.PROFILE_TEXT, ""));
		jo.put(Preferences.PROFILE_EMAIL,
				p.getString(Preferences.PROFILE_EMAIL, ""));
		jo.put(Preferences.PROFILE_CHAT,
				p.getString(Preferences.PROFILE_CHAT, ""));
		jo.put(Preferences.PROFILE_WEBSITE,
				p.getString(Preferences.PROFILE_WEBSITE, ""));
		jo.put(Preferences.PROFILE_ADDRESS,
				p.getString(Preferences.PROFILE_ADDRESS, ""));
		jo.put(Preferences.PROFILE_NOTE,
				p.getString(Preferences.PROFILE_NOTE, ""));

		jo.put(Preferences.PROFILE_MODIFIED,
				p.getString(Preferences.PROFILE_MODIFIED, NEVER_MODIFIED));
		jo.put(Preferences.PROFILE_PHOTO_MODIFIED,
				p.getString(Preferences.PROFILE_PHOTO_MODIFIED, NEVER_MODIFIED));

		jo.put(Preferences.PROFILE_PHOTO, !TextUtils.isEmpty(p.getString(
				Preferences.PROFILE_PHOTO, null)));

		jo.put(Preferences.PROFILE_SIGNATURE,
				p.getString(Preferences.PROFILE_SIGNATURE, ""));
		
		return jo;
	}

	private static String nvl(String s) {
		return (s == null ? "" : s);
	}

	public static long getProfileLastModified(Context ctx) {
		long date = 0;
		try {
			final SharedPreferences sp = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return Long.parseLong(sp
					.getString(PROFILE_MODIFIED, NEVER_MODIFIED));
		} catch (Exception e) {
		}
		return date;
	}

	public static long getProfilePhotoLastModified(Context ctx) {
		long date = 0;
		try {
			final SharedPreferences sp = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return Long.parseLong(sp.getString(PROFILE_PHOTO_MODIFIED,
					NEVER_MODIFIED));
		} catch (Exception e) {
		}
		return date;
	}

	public static boolean enableStatusbar(Context ctx) {
		final boolean defvalue = true;
		try {
			final SharedPreferences sp = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String key = ctx.getString(R.string.settings_statusbar_toggle_key)
					.toString();
			return sp.getBoolean(key, false);
		} catch (Exception e) {
		}
		return defvalue;
	}

	public static boolean isPeerStarred(Context ctx, String peer) {
		final SharedPreferences pref = ctx.getSharedPreferences(
				PREFS_FAVORITE_PEERS, Activity.MODE_PRIVATE);
		return isPeerStarred(pref, peer);
	}

	public static boolean isPeerStarred(SharedPreferences pref, String peer) {
		return (getPeerStar(pref, peer) != 0);
	}

	private static int getPeerStar(SharedPreferences pref, String peer) {
		final int defval = 0;
		try {
			return pref.getInt(peer, defval);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defval;
	}

	public static void starPeer(Context ctx, String peer, boolean star) {
		putPeerStar(ctx, peer, star ? 1 : 0);
	}

	/**
	 * star: 1, unstar: 0
	 * 
	 * @param ctx
	 * @param peer
	 * @param star
	 */
	private static void putPeerStar(Context ctx, String peer, int star) {
		final SharedPreferences pref = ctx.getSharedPreferences(
				PREFS_FAVORITE_PEERS, Activity.MODE_PRIVATE);
		try {
			Editor ed = pref.edit();
			ed.putInt(peer, star);
			ed.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static SharedPreferences getFavoritePeers(Context ctx) {
		final SharedPreferences pref = ctx.getSharedPreferences(
				PREFS_FAVORITE_PEERS, Activity.MODE_PRIVATE);
		return pref;
	}

	public static void removeStar(Context ctx, String peer) {
		final SharedPreferences pref = ctx.getSharedPreferences(
				PREFS_FAVORITE_PEERS, Activity.MODE_PRIVATE);
		pref.edit().remove(peer).commit();
	}

	public static String getFriends(Context ctx) {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		return sp.getString(MY_FRIENDS, null);
	}

	public static void putFriends(Context ctx, String emails) {
		final SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		Editor ed = pref.edit();
		if (TextUtils.isEmpty(emails)) {
			ed.remove(MY_FRIENDS);
		} else {
			ed.putString(MY_FRIENDS, emails);
		}
		ed.putBoolean(MY_FRIENDS_DIRTY, true);
		ed.commit();
	}

	public static boolean isFriendsDirty(Context ctx) {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		return sp.getBoolean(MY_FRIENDS_DIRTY, false);
	}

	public static String getProfileId(Context ctx) {
		return getProfileId(ctx, null);
	}
	
	public static String getProfileId(Context ctx, String def) {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		return sp.getString(PROFILE_ID, def);
	}
	
	public static void removeProfileId(Context ctx) {
		final SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		Editor ed = pref.edit();
		ed.remove(PROFILE_ID);
		ed.commit();
	}
	
	public static void putProfileId(Context ctx, String id, String updated) {
		final SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		Editor ed = pref.edit();
		ed.putString(PROFILE_ID, id);
		ed.putString(PROFILE_MODIFIED, updated);
		ed.commit();
	}
	
	public static void putProfileUpdated(Context ctx, String updated) {
		final SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		Editor ed = pref.edit();
		ed.putString(PROFILE_MODIFIED, updated);
		ed.commit();
	}
}
